package cn.ee.dfs.job.task;

import cn.ee.dfs.common.utils.RedisTool;
import cn.ee.dfs.esb.entity.ServiceInfo;
import cn.ee.dfs.esb.entity.UserService;
import cn.ee.dfs.esb.service.IServiceInfoService;
import cn.ee.dfs.esb.service.IUserServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Description:
 * Created by TWJ on 2019/8/26 0026 0025 下午 4:20 .
 */
@Slf4j
@Component
public class CacheTask {

    @Autowired
    private IUserServiceService userServiceService;

    @Autowired
    private IServiceInfoService serviceInfoService;

    @Autowired
    private RedisTool redisTool;

    public void updateUserServiceDayCallTimesAt0AM() {
        log.info("更新  updateUserServiceDayCallTimes ...");
        List<UserService> userServices = userServiceService.findUserServices(new UserService(UserService.APPLY_AGREE));
        userServices.forEach(us -> {
            String appKey = us.getAppKey();
            Map<Object, Object> map = redisTool.hmget(appKey);
            log.info("-- map ->" + map);
            Integer dayCall = (Integer) map.get("dayCall");
            Long userServiceId = (Long) map.get("userServiceId");
            if (dayCall != null) {
                redisTool.hdel(appKey, "dayCall");
                UserService userService = new UserService();
                userService.setUserServiceId(userServiceId);
                userServiceService.updateUserService(userService);
            }
        });
    }

    public void updateUserServiceRedisCache() {
        log.info("更新  updateUserServiceRedisCache ...");
        List<UserService> userServices = userServiceService.findUserServices(new UserService(UserService.APPLY_AGREE));
        userServices.forEach(us -> {
            String appKey = us.getAppKey();
            HashMap<String, Object> map = new HashMap<>();
            map.put("ip", us.getIps());
            map.put("frequency", us.getFrequency());
            map.put("serviceId", "svc:" + us.getServiceId());
            map.put("userServiceId", us.getUserServiceId());
            map.put("serviceName", us.getName());
            map.put("spName", us.getName());
            redisTool.hmset(appKey, map);
            log.info("  - key :"+appKey+" ->"+redisTool.hmget(appKey));
        });
    }

    public void updateServiceRedisCache() {
        log.info("更新  updateServiceRedisCache ...");
        List<ServiceInfo> sifs = serviceInfoService.findServiceInfos(new ServiceInfo());
        HashMap<String, Object> map = new HashMap<>();

        sifs.forEach(serviceInfo ->{
            map.put("name", serviceInfo.getName());
            map.put("spName", serviceInfo.getSpName());
            map.put("version", serviceInfo.getVersion());
            map.put("status", serviceInfo.getStatus());
            map.put("esbUrl", serviceInfo.getBindEsbPath());
            map.put("bznsUrl", serviceInfo.getBusinessPath());
            redisTool.hmset("svc:" + serviceInfo.getServiceId(), map);
        });

    }





    public void clearUserServiceDeprecatedCache() {
        log.info("更新  updateUserServiceRedisCache ...");

        Set<Object> allKeys = redisTool.allKeys();
        log.info("  allKeys ->"+allKeys);
//        List<UserService> userServices = userServiceService.findUserServices(new UserService(UserService.APPLY_AGREE));
//
//        userServices.forEach(us -> {
//            String appKey = us.getAppKey();
//            HashMap<String, Object> map = new HashMap<>();
//
//            redisTool.hmset(appKey, map);
//
//
//            log.info("  - key :"+appKey+" ->"+redisTool.hmget(appKey));
//        });
    }



}
